package com.kunminx.viabus.business;

import com.kunminx.viabus.business.constant.Configs;
import com.kunminx.viabus.repertory.model.AppDatabase;
import ohos.app.Context;

import com.kunminx.core.business.BaseBusiness;
import com.kunminx.core.bus.Result;
import com.kunminx.viabus.ResourceTable;
import com.kunminx.viabus.repertory.bean.NoteBean;
import com.kunminx.viabus.business.bus.INoteRequest;
import com.kunminx.viabus.business.bus.NoteBus;
import com.kunminx.viabus.business.constant.NoteResultCode;

import java.io.IOException;
import java.util.List;

import io.reactivex.ObservableEmitter;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;

/**
 * @author KunMinX
 * Create at 2018/9/19
 */
public class NoteBusiness extends BaseBusiness<NoteBus> implements INoteRequest {

    // private DataBaseAdapter mDataBase;
    private DatabaseHelper databaseHelper;
    private OrmContext ormContext = null;
    private Context mContext;

    public void init(Context context) {
        mContext = context;
        // mDataBase = new DataBaseAdapter();
        // 取代了 原来的 adapter
        databaseHelper = new DatabaseHelper(mContext);
        // mDataBase.init(context); 下面初始化数据数据库
        ormContext = databaseHelper.getOrmContext("AppDatabase", Configs.DB_PATH, AppDatabase.class);
    }

    @Override
    public void queryList() {
        handleRequest(new IAsync() {
            @Override
            public Result onExecute(ObservableEmitter<Result> e) throws IOException {
                OrmPredicates query = ormContext.where(NoteBean.class);
                List<NoteBean> list = ormContext.query(query);
                return new Result(NoteResultCode.QUERY_LIST, list);
            }
        });
    }

    @Override
    public void queryEntity(long id) {
        handleRequest(new IAsync() {
            @Override
            public Result onExecute(ObservableEmitter<Result> e) throws IOException {
                return null;
            }
        });
    }

    @Override
    public void insert(final NoteBean bean) {
        handleRequest(new IAsync() {
            @Override
            public Result onExecute(ObservableEmitter<Result> e) throws IOException {
                // long id = mDataBase.insertEntity(bean);
                ormContext.insert(bean);
                ormContext.flush();
                long id = bean.getRowId();
                System.out.println(id);
                if (id >= 0) {
                    bean.setId(id);
                    return new Result(NoteResultCode.INSERTED, bean);
                } else {
                    return new Result(NoteResultCode.FAILURE, mContext.getString(ResourceTable.String_tip_insert_error));
                }
            }
        });
    }

    @Override
    public void update(final NoteBean bean) {

        handleRequest(new IAsync() {
            @Override
            public Result onExecute(ObservableEmitter<Result> e) throws IOException {
                // int count =  mDataBase.updateEntity(bean);

                ormContext.update(bean);
                Boolean isSuccessed = ormContext.flush();

                if (isSuccessed) {
                    return new Result(NoteResultCode.UPDATED, bean);
                } else {
                    return new Result(NoteResultCode.FAILURE, mContext.getString(ResourceTable.String_tip_update_error));
                }
            }
        });
    }

    @Override
    public void delete(final NoteBean bean) {
        handleRequest(new IAsync() {
            @Override
            public Result onExecute(ObservableEmitter<Result> e) throws IOException {
                //int count = mDataBase.deleteEntity(bean); //
                ormContext.delete(bean);
                Boolean isSuccessed = ormContext.flush();
                if (isSuccessed) {
                    return new Result(NoteResultCode.DELETED, bean);
                } else {
                    return new Result(NoteResultCode.FAILURE, mContext.getString(ResourceTable.String_tip_delete_error));
                }
            }
        });
    }

    @Override
    public void onDestroy() {

    }
}
